﻿-- =============================================
-- Author:		zengfei
-- Create date: 03m/15/2012
-- Description:	更新采购订单主表
-- =============================================
CREATE PROCEDURE [dbo].[Usp_ModifyPurchaseMaster]
	@p_Action	char(1),
	@p_PurchaseNo	varchar(30),
	@p_VendorNo	varchar(8),
	@p_Dept	int,
	@p_ContractName	varchar(30),
	@p_ContractNo	varchar(30),
	@p_ReceivedWH	int,
	@p_Buyer	varchar(20),
	@p_PayAcccount	varchar(30),
	@p_PayAmount	numeric(15,2),
	@p_PayDate	datetime,
	@p_InDate	datetime,
	@p_EntryDate	datetime,
	@p_EntryEmployee	varchar(20),
	@p_InvoiceNo	varchar(30),
	@p_InvoiceDate	datetime,
	@p_ReferenceNo	varchar(30), 
	@p_Remark	varchar(120),
	@p_RecordID	int out,
	@p_UserID	varchar(60),
	@p_ErrMsg	varchar(100) out,
	@p_RFlag	smallint out
AS
begin transaction

if	@p_Action = 'A'
begin
	insert into PurchaseMaster (PurchaseNo, VendorNo, Dept, ContractName, ContractNo, ReceivedWH, Buyer, PayAccount, PayAmount, PayDate, InDate, EntryDate, EntryEmployee, InvoiceNo, InvoiceDate, ReferenceNo, Remark)
		values (@p_PurchaseNo, @p_VendorNo, @p_Dept, @p_ContractName, @p_ContractNo, @p_ReceivedWH, @p_Buyer, @p_PayAcccount, @p_PayAmount, @p_PayDate, @p_InDate, @p_EntryDate, @p_EntryEmployee, @p_InvoiceNo, @p_InvoiceDate, @p_ReferenceNo, @p_Remark)
		
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @p_RecordID = @@IDENTITY
end
else if	@p_Action = 'C'
begin
	update PurchaseMaster set VendorNo = @p_VendorNo,
								Dept = @p_Dept,
								ContractName = @p_ContractName,
								ContractNo = @p_ContractNo,
								ReceivedWH = @p_ReceivedWH, 
								Buyer = @p_Buyer, 
								PayAccount = @p_PayAcccount, 
								PayAmount = @p_PayAmount, 
								PayDate = @p_PayDate, 
								InDate = @p_InDate, 
								EntryDate = @p_EntryDate, 
								EntryEmployee = @p_EntryEmployee, 
								InvoiceNo = @p_InvoiceNo, 
								InvoiceDate = @p_InvoiceDate, 
								ReferenceNo = @p_ReferenceNo, 
								Remark = @p_Remark 
							where RecordID = @p_RecordID 
							
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end
else if	@p_Action = 'D'
begin
	delete from PurchaseMaster where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end

insert into PurchaseMaster_log (PurchaseNo, VendorNo, Dept, ContractName, ContractNo, ReceivedWH, Buyer, PayAccount, PayAmount, PayDate, InDate, EntryDate, EntryEmployee, InvoiceNo, InvoiceDate, ReferenceNo, Remark, RecordID, UserID, ModifyTime, [Action])
	values (@p_PurchaseNo, @p_VendorNo, @p_Dept, @p_ContractName, @p_ContractNo, @p_ReceivedWH, @p_Buyer, @p_PayAcccount, @p_PayAmount, @p_PayDate, @p_InDate, @p_EntryDate, @p_EntryEmployee, @p_InvoiceNo, @p_InvoiceDate, @p_ReferenceNo, @p_Remark, @p_RecordID, @p_UserID, GETDATE(), @p_Action)
	
if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle

commit transaction

set @p_RFlag = 1


ErrHandle:

if	@@TRANCOUNT > 0	rollback transaction

set @p_RFlag = -1

